//
//  CLComposePictureView.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/18.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit

//重用标记
private let CLComposePictureCell = "cell"
//发微博页面的配图
class CLComposePictureView: UICollectionView {

    //储存图片的数组
    lazy var images = [UIImage]()
    //点击加号按钮 执行的闭包
    var didSelectedAddClosure:(()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setUpUI()
         backgroundColor = UIColor.lightGrayColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpUI(){
        //注册
        registerClass(CLCompoesPictureViewCell.self , forCellWithReuseIdentifier: CLComposePictureCell)
        //设置代理  数据源
        dataSource = self
        delegate = self
        //默认隐藏配图
        hidden = true
    }
    //添加配图
    func addImage(image:UIImage) {
        //显示配图
        hidden = false
        //添加图片
        images.append(image)
        //重新加载数据
        reloadData()
    }
    //重写layoutsubview布局尺寸
    override func layoutSubviews() {
        super.layoutSubviews()
        //间距
        let itemMargin:CGFloat = 5
        //宽度
        let itemWidth = (width - 2 * itemMargin) / 3
        //获取flowlayout
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        //设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
    }
    

}
extension CLComposePictureView: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if images.count == 9 || images.count == 0 {
            return images.count
        }else {
            return images.count + 1
        }
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CLComposePictureCell, forIndexPath: indexPath) as! CLCompoesPictureViewCell
        //如果索引等于数组个数  则表示最后一张图片 
        if indexPath.item == images.count {
            cell.image = nil
        }else {
            cell.image = images[indexPath.item]
        }
        //设置闭包
        cell.deleteButtonClosure = {
            self.images.removeAtIndex(indexPath.item)
            //判断  如果数据源没有数据 则隐藏
            if self.images.count == 0{
                self.hidden = true
            }
            self.reloadData()
        }
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //表示加号按钮
        if indexPath.item == images.count {
            //取消选择 ，支持高亮图片
            collectionView.deselectItemAtIndexPath(indexPath, animated: true)
            didSelectedAddClosure?()
        }
    }
    
}

//配图cell 
class CLCompoesPictureViewCell:UICollectionViewCell {
    //点击删除按钮的闭包
    var deleteButtonClosure:(()->())?
    
    var image:UIImage?{
        didSet {
            //加号按钮的显示
            if image == nil {
                imageView.image = UIImage(named: "compose_pic_add")
            //隐藏删除按钮
                deleteButton.hidden = true
                //设置高亮图片
                imageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
            }else {
                imageView.image = image//选中的图片
                deleteButton.hidden = false
                imageView.highlightedImage = nil
            }

        }
    }
    
    
    
    //懒加载控件
    private lazy var imageView:UIImageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
    //删除按钮
    private lazy var deleteButton:UIButton = {
    
        let button = UIButton()
        button.addTarget(self, action: "deleteButtonAction", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        
        contentView.addSubview(imageView)
        contentView.addSubview(deleteButton)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(imageView)
            make.trailing.equalTo(imageView)
        }
    }
    
    
    //点击事件
    @objc private func deleteButtonAction(){
        deleteButtonClosure?()
    }
    
    
    
}

